package com.symaster.gameengine.e2d.renderer.particle;

import com.symaster.gameengine.api.ParticleBlockConfig;
import com.symaster.gameengine.core.Renderer;
import com.symaster.gameengine.e2d.Camera;

import java.awt.geom.Rectangle2D;
import java.util.HashSet;
import java.util.Set;

/**
 * @author yinmiao
 * @since 2024/9/19
 */
public class ParticleBlockRender extends Renderer {

    /**
     * 粒子列表
     */
    private final Set<ParticleBlockItemRenderer> particleBlockItemRenderers = new HashSet<>();
    /**
     * 相机
     */
    private Camera camera;
    /**
     * 生成粒子的配置
     */
    private ParticleBlockConfig particleBlockConfig;

    public ParticleBlockConfig getParticleBlockConfig() {
        return particleBlockConfig;
    }

    public void setParticleBlockConfig(ParticleBlockConfig particleBlockConfig) {
        this.particleBlockConfig = particleBlockConfig;
    }

    public Camera getCamera() {
        return camera;
    }

    public void setCamera(Camera camera) {
        this.camera = camera;
    }

    /**
     * @return 当前渲染器需要框架分配的显示的大小
     */
    @Override
    public int getWidth() {
        if (camera == null) {
            return 0;
        }
        Rectangle2D.Float sceneDisplayRect = camera.getSceneDisplayRect();
        double v = sceneDisplayRect.getWidth() * 1.2;
        return (int) v;
    }

    /**
     * @return 当前渲染器需要框架分配的显示的大小
     */
    @Override
    public int getHeight() {
        if (camera == null) {
            return 0;
        }
        Rectangle2D.Float sceneDisplayRect = camera.getSceneDisplayRect();
        double v = sceneDisplayRect.getHeight() * 1.2;
        return (int) v;
    }

    private final int[] intCache = new int[1];

    /**
     * 子组件使用, 在当前Fbo缓冲区里面绘制
     */
    @Override
    protected void draw() {
        if (particleBlockConfig == null) {
            return;
        }

        if (particleBlockConfig.getParticleLimit() <= 0) {
            return;
        }

        // 预计生成的粒子数量
        intCache[0] = particleBlockConfig.getParticleLimit() - particleBlockItemRenderers.size();

        if (intCache[0] < 1) {
            return;
        }

        for (int i = 0; i < intCache[0]; i++) {
            ParticleBlockItemRenderer allocation = allocation(particleBlockConfig);



        }
    }

    private ParticleBlockItemRenderer allocation(ParticleBlockConfig particleBlockConfig) {
        return null;
    }

}
